package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.RechargeAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.RechargeVerifyMobileDialog;
import com.lanlian.cayman.view.VerifyMobileDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.RechargeInfo;
import com.lanlian.commonlib.bean.ReqQueryRecharge;
import com.lanlian.commonlib.bean.ReqRecharge;
import com.lanlian.commonlib.bean.ReqSendSmsCode;
import com.lanlian.commonlib.bean.RespRecharge;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/5 18:26
 *     desc   : 充值
 *     version: 1.0
 * </pre>
 */
public class RechargeActivity extends BaseActivity {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;

    private ArrayList<RechargeInfo> mList = new ArrayList<>();
    private RechargeAdapter mAdapter;

    private RechargeVerifyMobileDialog mVerifyMobileDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge, true);
        initView();
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("充值");
        mLayRefresh = findViewById(R.id.lay_refresh);
        mRecycleView = findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getRechargeList();
            }
        });
    }

    private void initAdapter() {
        View view = LinearLayout.inflate(this, R.layout.lay_recharge_headview, null);
        mAdapter = new RechargeAdapter(R.layout.adapter_recharge_item, mList);
        mAdapter.addHeaderView(view);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_recharge);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                VolleyRequestUtils.getInstance().sendSmsCode(this.toString(), new ReqSendSmsCode(), new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        showToast("验证码已发送至您的手机");
                        mVerifyMobileDialog = new RechargeVerifyMobileDialog(RechargeActivity.this,
                                new RechargeVerifyMobileDialog.OnCompleteListener() {
                                    @Override
                                    public void onComplete(RechargeVerifyMobileDialog verifyMobileDialog, String content) {
                                        recharge(content, position);
                                    }
                                });
                        mVerifyMobileDialog.show();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        showToast(baseResponse.getMsg());
                    }
                });
            }
        });
    }

    private void recharge(String code, int position) {
        ReqRecharge reqRecharge = new ReqRecharge();
        reqRecharge.setCode(code);
        reqRecharge.setOrderNo(mList.get(position).getOrderNo());
        showLoadingDialog();
        VolleyRequestUtils.getInstance().recharge(this.toString(), reqRecharge, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                mVerifyMobileDialog.dismiss();
                mList.get(position).setStatus(1);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                if (!baseResponse.getMsg().contains("验证码")) {
                    mVerifyMobileDialog.dismiss();
                } else {
                    mVerifyMobileDialog.clear();
                }
                dismissLoadingDialog();
                showToast(baseResponse.getMsg());
            }
        });
    }

    private void getRechargeList() {
        VolleyRequestUtils.getInstance().queryRecharge(this.toString(), new ReqQueryRecharge(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mLayRefresh.finishRefresh();
                RespRecharge respRecharge = (RespRecharge) response;
                mList.clear();
                mList.addAll(respRecharge.getData());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mLayRefresh.finishRefresh();
                showToast(baseResponse.getMsg());
            }
        });
    }

}
